<template>
  <FormView :toolbar-attrs="{ buttons: ['back', 'slot'] }">
    <template #toolbar-button>
      <ToolbarButton icon="el-icon-printer" label="获取打印机列表" @click="getPrinterList" />
      <ToolbarButton icon="el-icon-printer" label="调整纸张尺寸" @click="adjustLabel" />
      <ToolbarButton icon="el-icon-printer" label="打印超高频RFID" @click="printRfid" />
      <ToolbarButton icon="el-icon-printer" label="打印超高频RFID(embed)" @click="printRfidEMBED" />
      <ToolbarButton icon="el-icon-printer" label="打印3次超高频RFID" @click="batchPrintRfid" />
      <ToolbarButton icon="el-icon-printer" label="读取超高频RFID" @click="getRfid" />
      <ToolbarButton icon="el-icon-printer" label="打印部队标签" @click="printArmySmallLabels()" />
      <ToolbarButton icon="el-icon-printer" label="打印非涉密小标签" @click="printSmallSecretLabels(false)" />
      <ToolbarButton icon="el-icon-printer" label="打印涉密小标签" @click="printSmallSecretLabels(true)" />
      <ToolbarButton icon="el-icon-printer" label="打印非涉密大标签" @click="printBigSecretLabels(false)" />
      <ToolbarButton icon="el-icon-printer" label="打印涉密大标签" @click="printBigSecretLabels(true)" />
    </template>
    <FormHolder>
      <FormItemGroup block-title="标签信息">
        <FormInput v-model="data.unitName" label="单位" />
        <FormInput v-model="data.name" label="资产名称" />
        <FormInput v-model="data.model" label="规格型号" />
        <FormInput v-model="data.code" label="资产编码" @input="val => (hex = alignHex(stringToHex(val)).join(''))" />
        <FormInput v-model="data.value" label="原    值" />
        <FormInput v-model="data.department" label="使用部门" />
        <FormInput v-model="data.time" label="取得时间" />
        <FormInput v-model="data.user" label="使用人" />
        <FormTextarea v-model="data.address" label="存放位置" />
      </FormItemGroup>
      <FormItemGroup block-title="十六进制与字符串互转">
        <FormInput v-model="hex" label="十六进制" @input="val => (data.code = hexToString(val))" />
        <FormInput v-model="data.code" label="字符串" @input="val => (hex = alignHex(stringToHex(val)).join(''))" />
      </FormItemGroup>
      <FormItemGroup v-for="item in printers" :key="item.printerUsbID" :block-title="`打印机信息(${item.printerName})`">
        <FormInput v-model="item.printerName" label="打印机名称" />
        <FormInput v-model="item.printerUsbID" label="打印机 UsbID" />
        <FormInput v-model="item.printerUsbPort" label="打印机 UsbPort" />
      </FormItemGroup>
    </FormHolder>
  </FormView>
</template>

<script>
  import { adjustLabel, getPrinters, getRfid, printRfid } from '@/util/label-print-api'
  import {
    hexToString,
    stringToHex,
    alignHex,
    getPrinterList,
    printLabel,
    printLabels,
    printSmallSecretLabels,
    printBigSecretLabels,
    printArmyLabels
  } from '@/util/label-print'

  const printIp = 'http://192.168.0.225:888/postek/print'

  export default {
    name: 'TestPrint',
    data() {
      const code = '2010105170918'
      return {
        printers: [{ printerName: '', printerUsbPort: '', printerUsbID: '' }],
        hex: stringToHex(code).join(''),
        data: {
          inAccount: false,
          category: '通用设备',
          logo: 'D:/logo.png',
          unitName: '广州海洋地质调查局',
          name: '便携式计算机便携式计算机便携式计算机',
          model: 'AS P453U 6500',
          code,
          time: '2023-03-01',
          department: '装备处',
          address: '调查船/海洋地质十二号/码头/张新雄/调查船/海洋地质十二号/码头/张新雄/',
          user: '张三',
          value: '4520元'
        }
      }
    },
    created() {
      this.getPrinter()
    },
    methods: {
      alignHex,
      stringToHex,
      hexToString,
      async adjustLabel() {
        const res = await adjustLabel()
        console.log(res.data)
      },
      async getPrinter() {
        const res = await getPrinters()
        this.printers = res.data.printerList
      },
      async getRfid() {
        const res = await getRfid(this.printers[0].printerUsbPort)
        console.log(res.data)
      },
      async printRfid() {
        const res = await printRfid(this.data)
        console.log(res.data)
      },
      getPrinterList() {
        getPrinterList(printIp, res => {
          console.log(res)
        })
      },
      batchPrintRfid() {
        const rows = []
        for (let i = 0; i < 3; i++) {
          rows.push(this.data)
        }
        printLabels({
          url: printIp,
          rows,
          transfer: function (row) {
            return row
          },
          cb: function (res) {
            console.log(res)
          },
          complete: function () {
            console.log('complete')
          }
        })
      },
      printRfidEMBED() {
        printLabel(printIp, this.data, res => {
          console.log(res)
        })
      },
      printBigSecretLabels(isSecret) {
        printBigSecretLabels({
          url: printIp,
          rows: [
            {
              // unitName: '广海局涉密设备标识',
              // name: '台式电脑',
              isSecret,
              purpose: '办公办公办公办公'
              // secret: '内部',
              // code: 'XXX-BM-M2-001',
              // mode: '型号',
              // department: '技术部',
              // user: '张三丰',
              // address: '1#',
              // time: '2023-04-14'
            }
          ],
          transfer: function (row) {
            return row
          },
          cb: function (res) {
            console.log(res)
          },
          complete: function () {
            console.log('complete')
          }
        })
      },
      printSmallSecretLabels(isSecret) {
        printSmallSecretLabels(
          {
            url: printIp,
            rows: [
              {
                // unitName: '广海局涉密设备标识',
                // name: '台式电脑',
                isSecret
                // secret: '内部',
                // purpose: '办公办公办公办公',
                // code: 'XXX-BM-M2-001',
                // mode: '型号',
                // department: '技术部',
                // user: '张三丰',
                // address: '1#',
                // time: '2023-04-14'
              }
            ],
            transfer: function (row) {
              return row
            },
            cb: function (res) {
              console.log(res)
            },
            complete: function () {
              console.log('complete')
            }
          },
          isSecret
        )
      },
      printArmySmallLabels() {
        printArmyLabels(
          {
            url: printIp,
            rows: [
              {
                // unitName: '广海局涉密设备标识',
                // name: '台式电脑',
                // secret: '内部',
                // purpose: '办公办公办公办公',
                // code: 'XXX-BM-M2-001',
                // mode: '型号',
                // department: '技术部',
                // user: '张三丰',
                // address: '1#',
                // time: '2023-04-14'
              }
            ],
            transfer: function (row) {
              return row
            },
            cb: function (res) {
              console.log(res)
            },
            complete: function () {
              console.log('complete')
            }
          }
        )
      }
    }
  }
</script>
